import { createTransport } from 'nodemailer'
import { codeTemplate } from './mailTemplate/index.js'

const user = 'keke7426e@163.com'
const name = 'ykxbc'
const pass = 'PASSWORD'

const smtpTransport = createTransport({
  host: 'smtp.163.com', //pop、smtp、imap
  port: 465,
  secure: true,
  auth: {
    user,
    pass,
  },
})

export const sentMail = ({ to, title, text, html }) => {
  return smtpTransport.sendMail({
    from: `${name} <${user}>`,
    to,
    subject: title,
    text,
    html,
  })
}

export const sendCodeMail = ({ to, time, code }) => {
  return sentMail({
    to,
    title: '验证码',
    html: codeTemplate({ code, time, username: to }),
  })
}
